/**
 * 
 */
package edu.towson.android.graphgrammar;

import java.io.File;
import java.io.IOException;

import edu.towson.android.graphgrammar.activity.ActivityException;
import edu.towson.android.graphgrammar.activity.AndriodActivitiesHandler;
import edu.towson.android.graphgrammar.util.FileUtil;

/**
 * @author Qian
 *
 */
public class Generator {

	private GeneratorInputProperties generatorInputProperties = null;

	/**
	 * Constructor
	 * 
	 * @param properties
	 */
	public Generator(GeneratorInputProperties properties) {
		this.generatorInputProperties = properties;
	}

	protected GeneratorInputProperties getGeneratorInputProperties() {
		return generatorInputProperties;
	}

	public void process() throws ActivityException,
			GraphGrammarProcessException {
		if (getGeneratorInputProperties() == null) {
			throw new IllegalArgumentException(
					"The input properties are required!");
		}
		try {
			if (generatorInputProperties.isAnalyzeActivity()) {
				processingActivities(generatorInputProperties);
			}

		} catch (ActivityException ae) {
			throw ae;
		}

	}

	private void processingActivities(
			GeneratorInputProperties generatorInputProperties)
			throws ActivityException {
		File manifiestFile = null;
		if (generatorInputProperties == null) {
			throw new IllegalArgumentException(
					"The input properties are missing");
		}

		try {
			try {
				manifiestFile = FileUtil.getExistingFileFromDirGivenFileName(
						generatorInputProperties.getProjectSrcPathName(),
						IConstants.MANIFEST_FILE_NAME);
			} catch (IOException ioe) {
				throw new ActivityException("Failed to find the manifest file",
						ioe);
			}
			if (generatorInputProperties.getVersion() == 1) {
				AndriodActivitiesHandler.handleWithVersion1(manifiestFile,
						generatorInputProperties
								.getGraphGrammerXMLResultFilePathFileName());

			}

		} catch (ActivityException ae) {
			throw ae;
		} catch (Exception e) {
			throw new ActivityException("failed to process", e);
		}
	}
}
